package m3u8

import (
	"gitee.com/kaylee595/m3u8/token"
	"strings"
)

type Resolution struct {
	Width, Height uint64
}

type HDCPLevel string

const (
	HDCPLevelNONE  HDCPLevel = "NONE"
	HDCPLevelTYPE0 HDCPLevel = "TYPE-0"
)

type MasterSegment struct {
	URI              string
	BandWidth        uint64     // 它表示变体流的峰值段比特率
	AverageBandWidth uint64     // 它表示变体流的平均段比特率
	Resolution       Resolution // 描述像素分辨率
	Codecs           []string   // 媒体样本类型
	FrameRate        float64    // 描述视频的最大帧率
	HDCPLevel        HDCPLevel  // TYPE-0 的值表示可能无法播放，除非输出受高带宽数字内容保护 (HDCP) 类型 0 [HDCP] 或等效保护。NONE 的值表示内容不需要输出复制保护。
	Audio            string
	Video            string
	SubTitles        string
}

const (
	keyNameBandWidth        = "BANDWIDTH"
	keyNameAverageBandWidth = "AVERAGE-BANDWIDTH"
	keyNameResolution       = "RESOLUTION"
	keyNameCodecs           = "CODECS"
	keyNameFrameRate        = "FRAME-RATE"
	keyNameHDCPLevel        = "HDCP-LEVEL"
	keyNameAudio            = "AUDIO"
	keyNameVideo            = "VIDEO"
	keyNameSubTitles        = "SUBTITLES"
)

func ParseMasterSegment(attrs token.Attributes, uri string) MasterSegment {
	x, y := attrs[keyNameResolution].FromResolution()
	return MasterSegment{
		URI:              uri,
		BandWidth:        attrs[keyNameBandWidth].FromUint(),
		AverageBandWidth: attrs[keyNameAverageBandWidth].FromUint(),
		Resolution:       Resolution{Width: x, Height: y},
		Codecs:           strings.Split(attrs[keyNameCodecs].FromString(), ","),
		FrameRate:        attrs[keyNameFrameRate].FromFloat(),
		HDCPLevel:        HDCPLevel(attrs[keyNameHDCPLevel].String()),
		Audio:            attrs[keyNameAudio].FromString(),
		Video:            attrs[keyNameVideo].FromString(),
		SubTitles:        attrs[keyNameSubTitles].FromString(),
	}
}
